/***************************************************************************
                          gm4lin.c  -  main program
                             -------------------
    begin                : Thu Dec 12 14:59:53 GST 2002
    copyright            : (C) 2003 by Mathias Bavay
    email                : mbavay@crtholdings.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include "gm4lin.h"

/* global variables						 */
/*the declaration order is important, for data alignement issues */

#ifdef SQL_LIBS
/* in case of an SQL logging, we will need some buffers */
char sql_query_into[SQL_QUERY_LENGHT],sql_query_values[SQL_QUERY_LENGHT],sql_query[SQL_QUERY_LENGHT];
#endif

int syslog_flag;
sig_atomic_t flag_alrm;						/* flag for SIGALRM */
sig_atomic_t flag_stop;						/* flag for the end of the recording */


/* ******************************************************************************************* */

int main(int argn,char *argv[]){
	struct configuration_struct settings;			/* storage of everything linked to configuration of the software */
	struct probe_descriptor probe[SERIAL_PORTS_MAX];	/* array of probe descriptor pointers */
	int sensitivity[SERIAL_PORTS_MAX];			/* table of probes sensitivities */
	int return_value;					/* store the status which will be given to "return" */
		
	/*****************************************************************************************************/
	/* initializations */
	return_value=EXIT_SUCCESS;
	flag_stop=0;
	
	#ifdef DEBUG
		printf("This is %s version %s (%s)\n",GM4LIN_NAME,GM4LIN_VERSION,GM4LIN_DATE);
	#endif
	#ifdef GETTEXT
		#ifdef DEBUG
		printf("Compiled with gettext support\n");
		#endif
		setlocale(LC_ALL,"");
        	bindtextdomain(GETTEXT_DOMAIN,GETTEXT_CAT_DIR);
		textdomain(GETTEXT_DOMAIN);
	#endif
	
	if(settings_init(argn,argv,&settings,sensitivity)== EXIT_FAILURE) return EXIT_FAILURE;
	/* are we going to run as a daemon ? If yes, do it ! */
	if(settings.daemon==EXIT_SUCCESS) daemonize();
	/* starting SYSLOG errors logging if requested */
	if(settings.syslog==EXIT_SUCCESS) {
		openlog(GM4LIN_NAME,LOG_PID,LOG_DAEMON);
		syslog(LOG_ERR,"Starting %s version %s (%s)",GM4LIN_NAME,GM4LIN_VERSION,GM4LIN_DATE);
	}
	printf(_("Starting program...\npress CTRL+C to stop\n"));
	/* changing directory to log_file_path if necessary as well as UID if requested */
	if(change_environement(settings)==EXIT_FAILURE) return EXIT_FAILURE;
	
	/* probes initialization */
	printf(_("************\nInitializing probes, discarding first %d readings...\n"), settings.discard);
	if(probes_init(probe,&settings,sensitivity)== EXIT_FAILURE) return EXIT_FAILURE;

	#ifdef SQL_LIBS
	/* MySQL initialization if necessary */
	if(settings.sql_log==EXIT_SUCCESS) {
		if(sql_init(&settings)== EXIT_FAILURE) return EXIT_FAILURE;	
	}
	#endif
	
	/*****************************************************************************************************/
	/* launching sequencer (timer+signal catching) */
	if(sequencer_init(settings)== EXIT_FAILURE) return EXIT_FAILURE;
	
	printf("************\n");
	flag_alrm=0;

	if (settings.pulse_mode==EXIT_FAILURE) {
		if(activity_recording(probe,&settings)==EXIT_FAILURE) return_value=EXIT_FAILURE;
	} 
	else {
		if(pulse_recording(probe,&settings)==EXIT_FAILURE) return_value=EXIT_FAILURE;
	}
	
	/*****************************************************************************************************/
	/* ending the program; stopping everything */
	#ifdef SQL_LIBS
	/* stopping MySQL if necessary */
	if(settings.sql_log==EXIT_SUCCESS) {
                mysql_stmt_close(settings.mysql_stmt);
		mysql_close(&settings.mysql);
	}
	#endif
	/* stopping probes */
	printf(_("\tStopping probes...\n"));
	if(stop_probes(probe,&settings)==EXIT_FAILURE) return_value=EXIT_FAILURE;
	
	if(settings.syslog==EXIT_SUCCESS) {
		syslog(LOG_ERR,_("Halting %s"),GM4LIN_NAME);
		closelog();
	}
	
	return return_value;
}
